#include "LiteApp.h"

/*
*********************************************************************************************************
* 函 数 名:   TASK_AT_Send
* 功能说明:   指令执行机：AT模块发送
* 形    参:   
* 返 回 值:   无
*********************************************************************************************************
*/
void TASK_AT_Send(void *pvParameters)
{
  u8  ret;
  u32 result;
  while(1)
  {
    switch (g_tAtHandler.eSendState)
    {
      //  空载状态
      case ATSEND_STATE_INIT:
        xQueueReceive(g_tAtHandler.xQueueAtCmd, &g_tAtHandler.AtCmdId, portMAX_DELAY);
        if(g_tAtHandler.AtCmdId>=AT)
        {
          g_tAtHandler.eSendState=ATSEND_STATE_SENDING;
          LITE_ATSEND_SetCmdid(g_tAtHandler.AtCmdId);       //Miaozi：设置发送命令
        }
        break;
      //  发送AT指令
      case ATSEND_STATE_SENDING:
        ret=LITE_ATSEND_Process();
        if(ret==AT_RET_OK)
        {
          g_tAtHandler.eSendState  = ATSEND_STATE_WAITING_RESPONSE;
        }
        else if(ret==AT_RET_DELAY)
        {
          result  = ulTaskNotifyTake(pdTRUE,pdMS_TO_TICKS(g_tAtHandler.tpAtSendHandler->pAtCmd->TimeSendDelay));  //Miaozi：等待通知&最长等待时间
          if(result>0)
          {
            LITE_ATSEND_SetState(ATSEND_STEP_FINISHCHECK);              //Miaozi：跳转至发送完成状态
          } 
          else
          {
            //Miaozi：超时重试
          }
        }
//        CLEAR_FLAG(g_tAtHandler.AtEvent,AT_EVENT_TX_SENDING);
//        g_tAtHandler.eSendState=ATSEND_STATE_INIT;    
        break;
      //  等待回复
      case ATSEND_STATE_WAITING_RESPONSE:
        //延时
        result  = ulTaskNotifyTake(pdTRUE,pdMS_TO_TICKS(g_tAtHandler.tpAtSendHandler->pAtCmd->TimeOut));  //Miaozi：等待通知&最长等待时间
        if(result>0)
        {
          LITE_LOG_Write("ATRECV_RET_OK\r\n",strlen("ATRECV_RET_OK\r\n")); 
        } 
        else
        {
          //Miaozi：超时重试
        }
        break;
      default:break;
    }
  }
}

/*
*********************************************************************************************************
* 函 数 名:   TASK_AT_Recv
* 功能说明:   指令执行机：AT模块接收
* 形    参:   
* 返 回 值:   无
*********************************************************************************************************
*/
void TASK_AT_Recv(void *pvParameters)
{
  LITE_ATRECV_T     *pAtRecvHandler= g_tAtHandler.tpAtRecvHandler;      //Miaozi：AT接收句柄
  BUFFER_T          *tpRxBuf        = pAtRecvHandler->pAtCmdRecvBuf;     //Miaozi: 接收逻辑缓存区
  MessageBufferHandle_t xMsgBufAtRecv = pAtRecvHandler->tpxMsgBufAtRecv;//Miaozi: 接收消息缓存区
  u16                 RxBytes;
  while(1)
  {
    switch(g_tAtHandler.eRecvState)
    {
      //  AT空闲状态
      case ATRECV_STATE_INIT:
        BUF_Write(tpRxBuf,"[AT] >> ",strlen("[AT] >> ")); //  拼接日志头  
        RxBytes = xMessageBufferReceive(xMsgBufAtRecv,BUF_GetTailInfo(tpRxBuf),BUF_GetRemainSize(tpRxBuf),portMAX_DELAY);
        tpRxBuf->length  +=RxBytes;
        BUF_Write(tpRxBuf,"\r\n",strlen("\r\n"));              //  拼接日志尾
        g_tAtHandler.eRecvState  = ATRECV_STATE_RECEIVEING;     //  接收到AT指令
        break;   
      //  AT接收状态  
      case ATRECV_STATE_RECEIVEING:
        LITE_LOG_Write(tpRxBuf->data,tpRxBuf->length);        //  通过LOG回显AT
        if(LITE_ATRECV_GetCmdId()) //  有待回复的AT指令
        {
          g_tAtHandler.eRecvState=ATRECV_STATE_PARSE_SYSMSG;
        }
        else
        {
          g_tAtHandler.eRecvState=ATRECV_STATE_PARSE_URCMSG;
        }
        break;
      //  AT解析状态：有待回复的AT消息
      case ATRECV_STATE_PARSE_SYSMSG:
        if(pAtRecvHandler->pAtCmd->on_receive != NULL)
        {
          u8 ret = pAtRecvHandler->pAtCmd->on_receive(tpRxBuf->data,tpRxBuf->length);
          if(ret==ATRECV_RET_OK)
          {      
            xTaskNotifyGive(xTaskHandle_AtSend);        //Miaozi:唤醒发送任务
            g_tAtHandler.eRecvState=ATRECV_STATE_INIT;
          }
        }
        break;
      //  AT解析状态：网络主动回传的消息
      case ATRECV_STATE_PARSE_URCMSG:
        LITE_ATRECV_UrcProecess(tpRxBuf->data,tpRxBuf->length);
        BUF_DelData(tpRxBuf);     //  删除缓存区数据
        g_tAtHandler.eRecvState=ATRECV_STATE_INIT;
        break;
      default :
        break;
    }
  }
}
/***************************** LiteBsp by Miaozi (END OF FILE) *********************************/

